package zhaolu.com.example.Delicacy.Activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import zhaolu.com.example.Delicacy.Entity.Customer;
import zhaolu.com.example.Delicacy.MySQL.CustomerDAO;
import zhaolu.com.example.Delicacy.R;
import zhaolu.com.example.Delicacy.Utils.CommonUtils;

public class ChangePassword extends AppCompatActivity {
    private EditText et_old;
    private EditText et_new;
    private Button bt_sure;
    private CustomerDAO customerDAO;
    private Handler mainHandler;

    private void initView() throws NoSuchAlgorithmException {
        et_old = findViewById(R.id.old_password);
        et_new = findViewById(R.id.new_password);
        bt_sure = findViewById(R.id.sure_change);
        customerDAO = new CustomerDAO();
        mainHandler = new Handler(getMainLooper());

        bt_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (view.getId() == R.id.sure_change) {
                    try {
                        doChange();
                    } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void doChange() throws NoSuchAlgorithmException {
        final String old_password = et_old.getText().toString().trim();
        final String new_password = et_new.getText().toString().trim();
        if (TextUtils.isEmpty(old_password)) {
            CommonUtils.showShortMsg(this, "请输入旧密码！");
            et_old.requestFocus();
        } else if (TextUtils.isEmpty(new_password)) {
            CommonUtils.showShortMsg(this, "请输入新密码！");
            et_new.requestFocus();
        } else {
            SharedPreferences sp = getSharedPreferences("login", Context.MODE_PRIVATE);
            String phone = sp.getString("username", null);
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(old_password.getBytes(StandardCharsets.UTF_8));
            String cipher_password = new BigInteger(messageDigest.digest()).toString(16);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final Customer item = customerDAO.getCustomerByUserAndPassword(phone, cipher_password);
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (item == null) {
                                CommonUtils.showShortMsg(ChangePassword.this, "旧密码不匹配！");
                            } else {
                                // TODO 修改用户密码（这里换成存储过程）。
                                messageDigest.update(new_password.getBytes(StandardCharsets.UTF_8));
                                String cipher_password = new BigInteger(messageDigest.digest()).toString(16);
                                item.setPassword(cipher_password);
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        customerDAO.editCustomer(item);
                                        mainHandler.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                CommonUtils.showShortMsg(ChangePassword.this, "修改成功！");
                                            }
                                        });
                                    }
                                }).start();
                            }
                        }
                    });
                }
            }).start();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.password_change);
        try {
            initView();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
}
